﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Vipesolutions.SharePoint.AddWebpartPickerExtension.HttpModule
{
    public class InjectFilter : Stream
    {
        #region contructors

        public InjectFilter(Stream stream)
        {
            _stream = stream;
        }

        #endregion

        #region fields

        private readonly Stream _stream;
        private bool _hasInject;

        #endregion

        #region Stream impl

        public override void Write(byte[] buffer, int offset, int count)
        {
            var stringBuffer = Encoding.UTF8.GetString(buffer);

            try
            {
                if (!_hasInject && !string.IsNullOrEmpty(stringBuffer))
                {
                    var injectPosition = stringBuffer.IndexOf("</head>");

                    if (injectPosition == -1)
                        injectPosition = stringBuffer.IndexOf("<link");

                    if (injectPosition == -1)
                        injectPosition = stringBuffer.IndexOf("<script");

                    if (injectPosition != -1)
                    {
                        stringBuffer = stringBuffer.Insert(injectPosition,
                                            "<script type=\"text/javascript\" language=\"javascript\" src=\"/_layouts/vipesolutions/jquery-1.4.js\"></script>" +
                                            "<script type=\"text/javascript\" language=\"javascript\" src=\"/_layouts/vipesolutions/vp.spwebpartpicker.js\"></script>" +
                                            "<script type=\"text/javascript\" language=\"javascript\">" +
                                            "   $(document).ready(function() {" +
                                                    "$().vpSpWebPartPickerEx();" +
                                            "   });" +
                                            "</script>");
                        _hasInject = true;
                    }
                }
            }
            catch (Exception e) { }

            var result = Encoding.UTF8.GetBytes(stringBuffer);

            _stream.Write(result, 0, result.Length);
        }

        public override bool CanRead
        {
            get { return false; }
        }

        public override bool CanSeek
        {
            get { return false; ; }
        }

        public override bool CanWrite
        {
            get { return true; }
        }

        public override void Flush()
        {
            _stream.Flush();
        }

        public override long Length
        {
            get { return _stream.Length; }
        }

        public override long Position
        {
            get
            {
                return _stream.Position;
            }
            set
            {
                _stream.Position = value;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return _stream.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return _stream.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            _stream.SetLength(value);
        }

        #endregion
    }
}